#include <boost/bind.hpp>
#include "event_notifier.h"

namespace dhd
{

   EventNotifier::EventNotifier( boost::asio::io_service & io_srv, EventCallback cb ) :
       writer(io_srv), reader(io_srv), on_event(cb)
   {
       boost::asio::local::connect_pair( reader, writer );
       
       reader.async_read_some(boost::asio::buffer(rbuff, sizeof(rbuff)), 
                              boost::bind(&EventNotifier::handle_read, 
                                          this, 
                                          boost::asio::placeholders::error,
                                          boost::asio::placeholders::bytes_transferred));
   }
   
   void EventNotifier::handle_read( const boost::system::error_code & error, std::size_t size )
   {
       on_event();
       
       reader.async_read_some(boost::asio::buffer(rbuff, sizeof(rbuff)), 
                              boost::bind(&EventNotifier::handle_read, 
                                          this, 
                                          boost::asio::placeholders::error,
                                          boost::asio::placeholders::bytes_transferred));
   }
   
   void EventNotifier::notify()
   {
       char b = 1;
       writer.send( boost::asio::buffer( &b, sizeof(b) ) );
   }
   
}// end namespace dhd

